package utils.encrypt;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private static String PRIVATEKEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL/IT3bzlSNjKQT4BfRBqrJ8e+yvaCrB0hZ4hg7WT7bPV0HhAVVD6J+/8Z9Folcjbl7L1eS0LCOt475iZb1egwnmwEcAASUl4vkUlEP7e2O/oMLm9P39fWv9/1umhGcXFinILxKC7/K2SeQfOBaGcNzVRUr0UOGOQoiX4va/KLylAgMBAAECgYACSdbLLCg2RfiNpmARA1wuwtC9ix/Qh7stYpDAakWvvBG9h8DjZu0PEO/svfY5GRAveVw58p+zekpLoIgejtcJIUuQdFN/+hEYkQ93AwfAJALgT0jVlnk5ZIHaK6nPN7YVnWzHlnoYCZZwZe758OVuTOVpcLSiKJRBiJE9dazVOQJBAOwpVm51lP78CQwzR9RH4cl6IPKZ9XtHWdlCbC3yjgc7UIGpRF0VJuMTCyaTpiYrqlokbmy+XVNgyJix/FAzY5kCQQDP5JmrODdxdL/ZUO/mjJucl0flBVLLb/x74I3K/omIxQWNmt3b43Sg0Gvv4gdpeQvl+s4TkKP4AjQXqiLDtsjtAkEAqWDMmNTEVE03wARXlC0qB3MMhnNd2VlHW8eEB0BcFVu5VgWs/oXZKivSZ4ymMeJHf/d5K40tKkvUE5A0QuFYoQJBAIFFdQiy/Ebggitv2L1T0a+domcW2FUIXcfp6kG/5WXWnY1gL3T8Cxm0gGm580HysyNewaYI2uzs8deFQ/jPQEECQAcCNQiQsCj/N0XWswYTrGEkbzCD5L8KwSxn99oJtXRRwQ2EYymc7IcAcr0PomH2NJf8IAKOu/2eZphforNPYwc=";
	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactoryToProcess,
			Properties props) throws BeansException {
	    String driverUrl = props.getProperty("jdbc.driverUrl");
		String username = props.getProperty("jdbc.user");
        String password = props.getProperty("jdbc.password");
        //解密jdbc.password属性值，并重新设置 
        try {
        	driverUrl = DecryptUtil.decryptRSA(driverUrl, PRIVATEKEY);
        	username = DecryptUtil.decryptRSA(username, PRIVATEKEY);
        	password = DecryptUtil.decryptRSA(password, PRIVATEKEY);
			props.setProperty("jdbc.driverUrl",driverUrl);
			props.setProperty("jdbc.user",username);
			props.setProperty("jdbc.password",password);
		} catch (Exception e) {
			logger.error("请检查属性配置文件！", e);
		}
		super.processProperties(beanFactoryToProcess, props);
	}

	public static void main(String[] args) throws Exception {
		String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/yE9285UjYykE+AX0QaqyfHvsr2gqwdIWeIYO1k+2z1dB4QFVQ+ifv/GfRaJXI25ey9XktCwjreO+YmW9XoMJ5sBHAAElJeL5FJRD+3tjv6DC5vT9/X1r/f9bpoRnFxYpyC8Sgu/ytknkHzgWhnDc1UVK9FDhjkKIl+L2vyi8pQIDAQAB";
		String driverUrl = "jdbc:mysql://127.0.0.1?useUnicode=true&useSSL=false&serverTimezone=UTC";
		String username = "root";
		String password = "root";

		String enDriverUrl = EncryptUtil.encryptRSA(driverUrl, publicKey);
		String enUsername = EncryptUtil.encryptRSA(username, publicKey);
		String enPassword = EncryptUtil.encryptRSA(password, publicKey);

		System.out.println("jdbc.driverUrl = " + enDriverUrl);
		System.out.println("jdbc.user = " + enUsername);
		System.out.println("jdbc.password = " + enPassword);

		System.out.println(DecryptUtil.decryptRSA(enDriverUrl, PRIVATEKEY));
		System.out.println(DecryptUtil.decryptRSA(enUsername, PRIVATEKEY));
		System.out.println(DecryptUtil.decryptRSA(enPassword, PRIVATEKEY));

	}
}
